home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
pascal
/
tpp112.zip
/
TPP.DOC
< prev
next >
Wrap
Text File
|
1992-04-18
|
19KB
|
445 lines
----------------------------------------------------------------------
04/18/92 Turbo Pascal Pre-Processor All Rights Reserved
Copyright (c) 1991-1992 by SupremeSoft Version 1.12
----------------------------------------------------------------------
Introduction
------------
What is TPP? TPP is a pre-processor designed to work with Turbo Pascal.
Many programmer's who are both C and Pascal programmers find that they
miss C's pre-processing capabilities.
In addition, professional Pascal programmers find it quite absurd to
declare constants for such mundane things as maximum values for array
indexes. This adds to the programs code size, as well as wasting precious
memory. Why declare a constant when you'll only be using it as a fixed
value, which the compiler can determine at compile-time as it is?
In C, all you need to do is use the pre-processor to define this
value. (The question of why use constants at all comes up. Especially if
they are useful, but wasteful of resources. Most languages use constants,
whereas C and C++ provide macro equivalents, and thus have no constant
keywords)
Finally, Turbo Pascal has a gotcha for all constants. They sit in your
data segment. Turbo Pascal, though smart in some ways, is not smart
enough to know that this constant is only a "hard coded" value, and will
thus not replace all references for that constant with its definition.
Besides this, there is a big caveat for using constants as initializers.
Reading your Turbo Pascal manual carefully, you will see that any constant
is only initialized ONCE. Even if it is declared in a procedure, it is only
initialized ONCE, and lives in the data segment and NOT the procedures local
call stack!
In comes TPP. TPP will take your simple constants and replace all
occurrences of that constant with its definition. Besides that, TPP can
be used to abbreviate coding declarations which will later be expanded at
compile time.
-----------------
How to use TPP
-----------------
TPP was designed to allow you to use the same source code that you would
normally use, but also allow you to use TPP keywords. In addition, TPP
works exactly like the Turbo Pascal Compiler. It will not recompile units
that do not need to be. (Although, it is not as smart as the Turbo
compiler as you will see later)
TPP scans your source, looking for a {#DEFINE} statement. This is the
signal to TPP that a macro definition is about to start. After this comes
the regular constant definition. For example:
Normal source: Const
UpperLimit = 3; {This is the upper limit}
LowerLimit = -1; {This is the lower limit}
To adapt to TPP: Const
{#DEFINE} UpperLimit = 3; {This is the upper limit}
{#DEFINE} LowerLimit = -1; {This is the lower limit}
Using this {#DEFINE} statement, your source will compile under Turbo as
usual, but can also be compiled using TPP. You do not need to keep two
different sets of source code.
That is all there is to it. Now, every occurrence of UpperLimit will be
replaced by a "3". You can also define string constants etc. In short any
constant that will work for the Turbo compiler will work for TPP.
Initialized constants, will *NOT* work in TPP, and should be avoided
during regular use of the Turbo compiler. Initialized constants have
numerous flaws and many, many gotchas. (Contact us for more detailed
information on why not to use initialized constants) For example, the
following is *NOT* legal for TPP:
Const
{#Define} LowerLimit : Byte = -1; {This is the lower limit}
You may use TPP to ease your coding a bit, by using the TPP macro feature
as you would normal text macros. For instance:
{#DEFINE} DOS = Intr(21h);
Then, in your code, you may say: DOS; {Call DOS}
and TPP will change this to : Intr(21h); {Call DOS}
Note that TPP can thus be used for other things than just constants!
{#DEFINE} statements may be used anywhere in your source code.
And of course, TPP is not case sensitive.
-------------
Limitations
-------------
TPP is not like the C macro pre-processor. You may not pass variables in
or out, nor do arithmetic expressions. For example, the following are
accepted, but are probably not what you want the macro to do:
1. {#DEFINE} TWICE(X) = X*X;
2. {#DEFINE} HIGH(X) = Odd(X) < Even (X);
3. {#DEFINE} Limits = 32 * MaxValue;
For definition 1, you would need to have a statement of: TWICE(Z);
Expansion then becomes: X*X;
You probably want: Z*Z;
For definition 2, you would need to have a statement of: HIGH(Z);
Expansion then becomes: Odd(X) < Even(X);
You probably want: Odd(Z) < Even(Z);
For definition 3, you would need to have a statement of: Limits;
Expansion then becomes: 32 * MaxValue;
Note: This is not evaluated at macro definition time, unlike in C.
However, Turbo will usually evaluate it at compile-time, so this
is actually a GOOD use for TPP.
You must be careful not to have the macro name anywhere else
in your source code, or else it will get expanded. (That is rather
obvious)
If you use the Const feature of TPP, be careful of the following:
Const
{#DEFINE} UpperLimit = 3; {This is the upper limit}
{#DEFINE} LowerLimit = -1; {This is the lower limit}
Type
.
.
When TPP processes your file, everything but the comments will be
stripped out, leaving:
Const
{This is the upper limit}
{This is the lower limit}
Type
.
.
The Turbo compiler, will *NOT* accept this. There must be something
following the CONST declaration. Be aware of this problem. The only real
work around is to comment out the CONST statement. (This may changed
in later versions of TPP)
-------------
How TPP works
--------------
TPP will scan your source file looking for {#DEFINE} statements or macro
names. It handles Units just like the Turbo compiler, and looks for a
USES keyword. If it finds one, it suspends processing of the current file
and looks at the last file in the USES list. (Just as the Turbo compiler
does) If the TPU for that file is older than the file, TPP will process
that file. This is done for all Units in the USES list.
You may define a macro in one unit, and use it in another, as long as
that unit is listed in the second file's USES list. Confused? Follow the
same programming style as you do regularly. TPP needs no change in
programming! It will allow you to treat your source the same way as Turbo
does. Consider the following:
Unit 1;
...
{Not the next line can be in either the interface OR the implementation
section. Either way it will be visible to other units using this unit}
{#DEFINE} Upper = 99;
Unit 2;
Uses Unit 1;
...
Var t : array[1..Upper] of real;
Even though Unit2 doesn't declare Upper, it can still use it.
------------------------------------------------------------------
One major flaw in TPP:
If Unit1 does NOT need re-compiling, then the macro Upper will never
get defined for TPP!
-------------------------------------------------------------------
Once TPP encounters a macro, it stores the macro name, and its
corresponding definition. In all later files, any time the macro name is
detected, it will get replaced with the definition. Once TPP reads the
macro, it will remove it from the source file. Thus causing the Turbo
compiler to "ignore" the macro, since it isn't there.
TPP will scan all the files needed, but will only produce a new file if
any changes are made to the original source. Depending on the mode of
operation, this new file will either overwrite the original source file,
be named DUM?.PAS or wil